// 02local/02shopHome/shopHome.js
var co = require('../../resource/js/common.js')
var app = getApp()
var _this
Page({

   /* 页面的初始数据*/
   data: {
      //商家信息
      Biz_ID: '',
      shopInfo: '',
      // 评分
      score: '',
      star: [],
      authConfirm: 0,
      // 标签栏
      tabbar: ['店铺信息', '店铺内景', '店铺评价'],
      idx: 0,
      // 轮播图
      indicatorDots: false,
      autoplay: true,
      interval: 3000,
      duration: 1000,
      // 店铺信息循环
      shopItem: [
         { img: '/resource/icon/shopHome/bookmark.png', info: '商家名称：' },
         { img: '/resource/icon/shopHome/address.png', info: '商家位置：' },
         { img: '/resource/icon/shopHome/time.png', info: '营业时间：' },
         { img: '/resource/icon/shopHome/phone.png', info: '店铺电话：' }
      ],
      // 屏幕高度
      realHeight: '',
      // 点击点菜按钮动画
      isPopping: true,//是否已经弹出 
      animPlus: {},//旋转动画 
      animCollect: {},//item位移,透明度 
      //   animTranspond: {},//item位移,透明度 
      animInput: {},//item位移,透明度 
      // 商家评论
      comment: '',
      //用户评价星星
      userStar: [],
      // wifi,信用卡显示隐藏
      wifiName: '',
      wifiPassWord: '',
      //自定义弹窗参数
      setGuestNumShow: 0,
      numArray: [
         [1, 2, 3, 4],
         [5, 6, 7, 8],
         [9, 10, 11, 12],
         [13, 14, 15, 0]
      ],
   },

   /* 生命周期函数--监听页面加载*/
   onLoad: function (options) {
      _this = this
      // 获取设备屏幕宽度
      var w_h = wx.getSystemInfoSync().windowHeight - 195
      var Biz_ID = options.Biz_ID
      app.getShopInfo(Biz_ID, function (res) {
         var d = _this.data.shopItem
         d[0].da = res.data.Biz_Account
         d[1].da = res.data.Biz_Address
         d[2].da = '9:00~22:00'
         d[3].da = res.data.Biz_Phone
         _this.setData({
            shopInfo: res.data,
            shopItem: d,
            score: res.data.Score,
            wifiName: res.data.Wifi_Name,
            wifiPassWord: res.data.Wifi_Pass
         })
         _this.showStart(res.data.Score, 1)
      })
      this.setData({
         Biz_ID: Biz_ID,
         realHeight: w_h
      })
   },

   // 综合评分显示星星
   showStart(Score, count) {
      const LENGTH = 5;
      const CLS_ON = "../../resource/icon/star/on.png"
      const CLS_OFF = "../../resource/icon/star/off.png"
      const CLS_HALF = "../../resource/icon/star/half.png"
      //评分判断
      let starArray = []
      let score = Math.floor(Score * 2) / 2//lpData.data.comment.sRate表示获取当前评分，例如：4.65等
      let hasDecimal = score % 1 !== 0
      let integer = Math.floor(score)
      for (let i = 0; i < integer; i++) {
         starArray.push(CLS_ON)
      }
      if (hasDecimal) {
         starArray.push(CLS_HALF)
      }
      while (starArray.length < LENGTH) {
         starArray.push(CLS_OFF)
      }
      if (count == 1) {
         //存入星星数组
         _this.setData({
            star: starArray
         })
      } else if (count == 2) {
         let a = this.data.userStar
         a.push(starArray)
         _this.setData({
            userStar: a
         })
      }
   },
   //点击改变标签栏样式
   changeTab: function (e) {
      var id = e.currentTarget.id
      this.setData({ idx: id })
      // 店铺评论
      if (id == 2) {
         var param = {
            Biz_ID: this.data.Biz_ID
         }
         co.getShareApi('product/Pmg/getComment', param, function (res) {
            // console.log("【返回信息】===",res.data)
            if (res.code == 0) {
               _this.setData({
                  comment: res.data
               })
               var len = res.data.length
               for (var i = 0; i < len; i++) {
                  _this.showStart(res.data[i].Score, 2)
               }
            }
         })
      }
   },
   //点菜按钮动画
   plus: function () {
      if (this.data.isPopping) {
         //缩回动画 
         this.popp();
         this.setData({
            isPopping: false
         })
      } else if (!this.data.isPopping) {
         //弹出动画 
         this.takeback();
         this.setData({
            isPopping: true
         })
      }
   },
   //弹出动画 
   popp: function () {
      //plus顺时针旋转 
      var animationPlus = wx.createAnimation({
         duration: 500,
         timingFunction: 'ease-out'
      })
      var animationcollect = wx.createAnimation({
         duration: 500,
         timingFunction: 'ease-out'
      })
      var animationTranspond = wx.createAnimation({
         duration: 500,
         timingFunction: 'ease-out'
      })
      var animationInput = wx.createAnimation({
         duration: 500,
         timingFunction: 'ease-out'
      })
      animationPlus.rotateZ(360).step();
      animationcollect.translate(-30, -70).rotateZ(360).opacity(1).step();
      animationInput.translate(-80, 0).rotateZ(360).opacity(1).step();
      this.setData({
         animPlus: animationPlus.export(),
         animCollect: animationcollect.export(),
         animInput: animationInput.export(),
      })
   },
   //收回动画 
   takeback: function () {
      //plus逆时针旋转 
      var animationPlus = wx.createAnimation({
         duration: 500,
         timingFunction: 'ease-out'
      })
      var animationcollect = wx.createAnimation({
         duration: 500,
         timingFunction: 'ease-out'
      })
      var animationInput = wx.createAnimation({
         duration: 500,
         timingFunction: 'ease-out'
      })
      animationPlus.rotateZ(0).step();
      animationcollect.translate(0, 0).rotateZ(0).opacity(0).step();
      animationInput.translate(0, 0).rotateZ(0).opacity(0).step();
      this.setData({
         animPlus: animationPlus.export(),
         animCollect: animationcollect.export(),
         animInput: animationInput.export(),
      })
   },
   // 预约
   collect: function () {
      wx.navigateTo({
         url: '/02local/05reserveSeat/reserveSeat?Biz_ID=' + this.data.Biz_ID,
      })
   },
   // 选桌点餐
   input: function () {
      this.setData({
         setGuestNumShow: 1
      })
   },
   //设置人数
   setGuestNum: function (e) {
      var num = e.currentTarget.id
      if (num > 0) {
         this.setData({ setGuestNumShow: 0 })
         wx.navigateTo({
            url: '/02local/06selectDesk/selectDesk?Biz_ID=' + this.data.Biz_ID + '&Guest_Num=' + num + '&value=' + '点餐'
         })
      } else {
         this.setData({ numArray: app.reSetNum(_data.numArray, num, 15) })
         // this.reSetNum(num)
      }
   },
   //  //重设数字
   //  reSetNum: function (num) {
   //      var nA = this.data.numArray
   //      for (var i = 0; i < nA.length; i++) {
   //          var nA2 = nA[i]
   //          for (var j = 0; j < nA2.length; j++) {
   //              if (num == 0) {
   //                  if (nA[i][j] == 15) nA[i][j] = -1
   //                  else if (nA[i][j] > 15) nA[i][j] += 14
   //                  else if (nA[i][j] > 0) nA[i][j] += 15
   //              } else {
   //                  if (nA[i][j - 1] == 14) nA[i][j] = 15
   //                  else if (nA[i][j] > 29) nA[i][j] -= 14
   //                  else if (nA[i][j] > 0) nA[i][j] -= 15
   //              }
   //          }
   //      }
   //      this.setData({ numArray: nA })
   //  },
   // 隐藏弹窗
   maskHide: function () {
      this.setData({
         setGuestNumShow: 0,
         // regShow: 0,
         // call: 0,
         // authConfirm: 0
      })
   }
})